package com.domain.controller;

import com.domain.common.AppLogger;
import com.domain.common.Comments;
import com.domain.common.Result;
import com.domain.service.ContactsService;
import com.domain.utils.JwtUtils;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 联系人
 */
@Controller
@RequestMapping("/contacts")
public class ContactsController {

    @Autowired
    private ContactsService contactsService;
    /**
     * 日志
     */
    private static Logger logger = AppLogger.getLogger(ContactsController.class.getSimpleName(), Level.INFO.toString(), true);

    /**
     * 同步通讯录
     *
     * @param userToken 用户token
     * @param userId    用户token
     * @param jsonStr   通讯录信息{"phoneNum":"","":""}
     * @return
     */
    @RequestMapping(value = "/synchronizationContacts", method = RequestMethod.POST)
    @ResponseBody
    public Result synchronizationContacts(String userToken, String userId, String jsonStr) {
        try {
            //判断用户是否登录
            if (!JwtUtils.veryfiy(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }
            return contactsService.synchronizationContacts(userId, jsonStr);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统错误");
        }
    }


    /**
     * 获取通讯录列表()搜索条件
     *
     * @param userToken 用户token
     * @param userId    用户id
     * @param
     * @param request
     * @return
     */
    @RequestMapping(value = "/getContactsList", method = RequestMethod.POST)
    @ResponseBody
    public Result getContactsList(String userToken, String userId, String phoneNum, String name, String pageNum, String pageSize, HttpServletRequest request) {
        try {
            //判断用户是否登录
            if (!JwtUtils.veryfiy(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }
            return contactsService.getContactsList(userId, phoneNum, name, pageNum, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统错误");
        }
    }

    /**
     * 获取加好友请求列表
     *
     * @param userToken
     * @param usersId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/getNewFriendsRequestList", method = RequestMethod.POST)
    @ResponseBody
    public Result getNewFriendsRequestList(String userToken, String usersId, String pageNum, String pageSize) {
        try {
            //判断用户是否登录
            /*if (!JwtUtils.veryfiy(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }*/
            return contactsService.getNewFriendsRequestList(usersId, pageNum, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统错误");
        }
    }

    /**
     * 同意加好友
     *
     * @param userToken
     * @param userId
     * @param friendsId
     * @return
     */
    @RequestMapping(value = "/agreeAddFriends", method = RequestMethod.POST)
    @ResponseBody
    public Result agreeAddFriends(String userToken, String uafId, String userId, String friendsId) {
        try {
            //判断用户是否登录
            if (!JwtUtils.veryfiy(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }
            return contactsService.agreeAddFriends(uafId, userId, friendsId);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("操作失败");
        }
    }

    /**
     * 忽略加好友
     *
     * @param userToken
     * @return
     */
    @RequestMapping(value = "/deleteAddFriends", method = RequestMethod.POST)
    @ResponseBody
    public Result deleteAddFriends(String userToken, String uafId) {
        try {
            //判断用户是否登录
            if (!JwtUtils.veryfiy(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }
            return contactsService.deleteAddFriends(uafId);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("操作失败");
        }
    }


    /**
     * 邀请好友（通讯录好友发短信）
     *
     * @return
     */
    @RequestMapping(value = "/joinAppSendMesssage", method = RequestMethod.POST)
    @ResponseBody
    public Result joinAppSendMesssage(String userToken, String userId, String phoneNum, HttpServletRequest request) {
        try {
            //判断用户是否登录
            if (!JwtUtils.veryfiy(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }
            return contactsService.joinAppSendMesssage(userId, phoneNum);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统错误");
        }

    }
}
